package com.youku.adSDK.model
{
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.ADContext;
   import flash.net.URLLoader;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.dynamicflash.util.Base64;
   import com.youku.adSDK.ADConstant;
   import flash.system.Capabilities;
   import flash.net.URLRequest;
   import com.youku.adSDK.model.vo.GroupData;
   import com.youku.adSDK.model.vo.ADData;
   
   public class ADProxy extends Object
   {
      
      public function ADProxy() {
         super();
         this._eventDispather = ADEventDispather.getInstance();
      }
      
      protected var _eventDispather:ADEventDispather;
      
      protected var _adContext:ADContext;
      
      public function loadData(param1:String, param2:String, param3:ADContext, param4:Number = 0, param5:Number = 0, param6:Number = 0) : void {
         var _loc10_:uint = 0;
         var _loc12_:String = null;
         var _loc13_:String = null;
         var _loc14_:* = false;
         var _loc15_:* = false;
         var _loc16_:* = false;
         var _loc17_:* = false;
         this._adContext = param3;
         var _loc7_:URLLoader = new URLLoader();
         _loc7_.addEventListener(Event.COMPLETE,this.onLoaderComplete);
         _loc7_.addEventListener(IOErrorEvent.IO_ERROR,this.onLoaderIOError);
         _loc7_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoaderSecurityError);
         var _loc8_:* = "";
         var _loc9_:Number = 0;
         if(this._adContext.hasUserData)
         {
            _loc9_ = Number(this._adContext.vip);
         }
         _loc8_ = _loc8_ + ("vip=" + _loc9_);
         if(_loc9_ != 0)
         {
            _loc8_ = _loc8_ + ("&uk=" + Base64.encode(String(this._adContext.uk)));
         }
         _loc8_ = _loc8_ + ("&site=" + "1");
         if(param1 == ADConstant.FRONT_AD)
         {
            _loc10_ = 1;
            _loc12_ = new Date().time.toString();
            _loc13_ = param6 + "|" + _loc12_ + "|" + this._adContext.vidEncoded;
            _loc13_ = Base64.encode(_loc13_);
            _loc8_ = _loc8_ + ("&rt=" + _loc13_);
         }
         else if(param1 == ADConstant.BACK_AD)
         {
            _loc10_ = 2;
            _loc14_ = this._adContext.winType == "interior";
            _loc15_ = this._adContext.isContinuous == true;
            _loc16_ = this._adContext.hasNextVideo;
            _loc17_ = (this._adContext.rootShowLoop) && (this._adContext.isFSOLoop);
            if((_loc14_) && (_loc15_) && (_loc16_) || (_loc17_))
            {
               _loc8_ = _loc8_ + "&ctu=1";
            }
            else
            {
               _loc8_ = _loc8_ + "&ctu=0";
            }
         }
         else if(param1 == ADConstant.INSERT_AD)
         {
            _loc10_ = 3;
            _loc8_ = _loc8_ + ("&ps=" + param4);
            _loc8_ = _loc8_ + ("&pt=" + param5);
         }
         else if(param1 == ADConstant.PAUSE_AD)
         {
            _loc10_ = 4;
         }
         else if(param1 == ADConstant.OVERLAY_AD)
         {
            _loc10_ = 5;
            _loc8_ = _loc8_ + ("&ps=" + param4);
         }
         
         
         
         
         _loc8_ = _loc8_ + ("&p=" + _loc10_);
         _loc8_ = _loc8_ + ("&vl=" + this._adContext.totalTime);
         _loc8_ = _loc8_ + ("&fu=" + (this._adContext.isInFullScreen == false?0:1));
         if(!(this._adContext.ct == null) && !(this._adContext.ct == ""))
         {
            _loc8_ = _loc8_ + ("&ct=" + this._adContext.ct);
         }
         if(!(this._adContext.cs == null) && !(this._adContext.cs == ""))
         {
            _loc8_ = _loc8_ + ("&cs=" + this._adContext.cs);
         }
         _loc8_ = _loc8_ + ("&paid=" + (!this._adContext.hasShowData?0:this._adContext.isVideoPaid));
         _loc8_ = _loc8_ + ("&s=" + (!this._adContext.hasShowData?"":this._adContext.showID));
         _loc8_ = _loc8_ + ("&td=" + this._adContext.sourceVid);
         _loc8_ = _loc8_ + ("&sid=" + this._adContext.sid);
         _loc8_ = _loc8_ + ("&v=" + this._adContext.videoId);
         _loc8_ = _loc8_ + ("&wintype=" + this._adContext.winType);
         _loc8_ = _loc8_ + ("&u=" + this._adContext.u);
         _loc8_ = _loc8_ + ("&vs=" + "1.0");
         _loc8_ = _loc8_ + ("&rst=" + "flv");
         _loc8_ = _loc8_ + ("&partnerid=" + this._adContext.partnerId);
         _loc8_ = _loc8_ + ("&dq=" + this._adContext.defaultQuality);
         if(this._adContext.hasPartnerData)
         {
            _loc8_ = _loc8_ + ("&atm=" + this._adContext.atm);
         }
         if(this._adContext.winType == "exterior")
         {
            _loc8_ = _loc8_ + ("&emb=" + this._adContext.embedId);
         }
         if((this._adContext.tags) && (this._adContext.tags.length))
         {
            _loc8_ = _loc8_ + ("&k=" + escape(this._adContext.tags.join("|").toString()));
         }
         else
         {
            _loc8_ = _loc8_ + "&k=";
         }
         if(!(this._adContext.adext == null) && !(this._adContext.adext == ""))
         {
            _loc8_ = _loc8_ + ("&adext=" + this._adContext.adext);
         }
         if(this._adContext.ev != -1)
         {
            _loc8_ = _loc8_ + ("&ev=" + String(this._adContext.ev));
         }
         _loc8_ = _loc8_ + ("&os=" + encodeURIComponent(Capabilities.os));
         var _loc11_:* = "0";
         if(this._adContext.hasFolder)
         {
            _loc11_ = String(this._adContext.folderId);
         }
         _loc8_ = _loc8_ + ("&d=" + _loc11_);
         if(this._adContext.hasTrial)
         {
            _loc8_ = _loc8_ + ("&tt=" + this._adContext.trialType);
         }
         _loc8_ = _loc8_ + ("&ti=" + encodeURIComponent(this._adContext.title));
         _loc7_.load(new URLRequest(param2 + _loc8_));
      }
      
      protected function onLoaderSecurityError(param1:SecurityErrorEvent) : void {
         this.onLoaderError(null);
      }
      
      protected function onLoaderIOError(param1:IOErrorEvent) : void {
         this.onLoaderError(null);
      }
      
      public function onLoaderComplete(param1:Event) : void {
      }
      
      public function onLoaderError(param1:Event) : void {
      }
      
      public function parseGroupData(param1:Object) : GroupData {
         var _loc6_:Array = null;
         var _loc7_:* = 0;
         var _loc8_:ADData = null;
         var _loc9_:Array = null;
         var _loc10_:* = 0;
         var _loc11_:ADData = null;
         var _loc12_:Array = null;
         var _loc13_:* = 0;
         var _loc14_:ADData = null;
         if(!param1)
         {
            return null;
         }
         var _loc2_:GroupData = new GroupData();
         _loc2_.P = param1.P;
         if(param1.hasOwnProperty("SKIP"))
         {
            _loc2_.SKIP = param1.SKIP;
         }
         var _loc3_:Array = param1.JS as Array;
         if((_loc3_) && _loc3_.length > 0)
         {
            _loc6_ = [];
            _loc7_ = 0;
            while(_loc7_ < _loc3_.length)
            {
               _loc8_ = this.parseADData(_loc3_[_loc7_] as Object);
               _loc6_.push(_loc8_);
               _loc7_++;
            }
            _loc2_.JS = _loc6_;
         }
         var _loc4_:Array = param1.VAL as Array;
         if((_loc4_) && _loc4_.length > 0)
         {
            _loc9_ = [];
            _loc10_ = 0;
            while(_loc10_ < _loc4_.length)
            {
               _loc11_ = this.parseADData(_loc4_[_loc10_] as Object);
               _loc9_.push(_loc11_);
               _loc10_++;
            }
            _loc2_.VAL = _loc9_;
         }
         var _loc5_:Array = param1.SEED as Array;
         if((_loc5_) && _loc5_.length > 0)
         {
            _loc12_ = [];
            _loc13_ = 0;
            while(_loc13_ < _loc5_.length)
            {
               _loc14_ = this.parseADData(_loc5_[_loc13_] as Object);
               _loc12_.push(_loc14_);
               _loc13_++;
            }
            _loc2_.SEED = _loc12_;
         }
         if(param1.ATS)
         {
            _loc2_.ATS = param1.ATS as Array;
         }
         if(param1.hasOwnProperty("PS"))
         {
            _loc2_.PS = param1.PS;
         }
         this.traceData(_loc2_);
         return _loc2_;
      }
      
      public function parseADData(param1:Object) : ADData {
         if(!param1)
         {
            return null;
         }
         var _loc2_:ADData = new ADData();
         _loc2_.AT = param1.AT;
         _loc2_.RST = param1.RST;
         _loc2_.RS = param1.RS;
         _loc2_.AL = param1.AL;
         _loc2_.SU = param1.SU;
         _loc2_.SUS = param1.SUS;
         _loc2_.SUE = param1.SUE;
         _loc2_.CUM = param1.CUM;
         _loc2_.CU = param1.CU;
         _loc2_.VT = param1.VT;
         _loc2_.VC = param1.VC;
         _loc2_.W = param1.W;
         _loc2_.H = param1.H;
         _loc2_.CF = param1.CF;
         _loc2_.JS = param1.JS;
         _loc2_.BRS = param1.BRS;
         _loc2_.LOC = param1.LOC;
         return _loc2_;
      }
      
      protected function traceData(param1:GroupData) : void {
         var _loc2_:* = 0;
         var _loc3_:ADData = null;
         var _loc4_:Object = null;
         var _loc5_:Object = null;
         var _loc6_:Object = null;
         var _loc7_:Object = null;
         var _loc8_:* = 0;
         var _loc9_:ADData = null;
         var _loc10_:Object = null;
         var _loc11_:Object = null;
         var _loc12_:Object = null;
         var _loc13_:Object = null;
         var _loc14_:* = 0;
         var _loc15_:ADData = null;
         var _loc16_:Object = null;
         var _loc17_:Object = null;
         var _loc18_:Object = null;
         var _loc19_:Object = null;
         trace("parseGroupData ----------------------------------------------- //");
         trace("P:",param1.P);
         trace("********** JS:",param1.JS,"**********");
         if(param1.JS)
         {
            _loc2_ = 0;
            while(_loc2_ < param1.JS.length)
            {
               trace("---- JS dataArray at index: ",_loc2_,"----");
               _loc3_ = param1.JS[_loc2_] as ADData;
               trace("AT:\t",_loc3_.AT);
               trace("RST:\t",_loc3_.RST);
               trace("RS:\t",_loc3_.RS);
               trace("AL:\t",_loc3_.AL);
               trace("CU:\t",_loc3_.CU);
               trace("VT:\t",_loc3_.VT);
               trace("VC:\t",_loc3_.VC);
               trace("W:\t",_loc3_.W);
               trace("H:\t",_loc3_.H);
               trace("CF:\t",_loc3_.CF);
               trace("JS:\t",_loc3_.JS);
               trace("BRS:\t",_loc3_.BRS);
               trace("SU:\t",_loc3_.SU);
               trace("LOC:\t",_loc3_.LOC);
               for each(_loc4_ in _loc3_.SU)
               {
                  trace("\t","U:",_loc4_.U);
                  trace("\t","T:",_loc4_.T);
               }
               trace("SUS:\t",_loc3_.SUS);
               for each(_loc5_ in _loc3_.SUS)
               {
                  trace("\t","U:",_loc5_.U);
               }
               trace("SUE:\t",_loc3_.SUE);
               for each(_loc6_ in _loc3_.SUE)
               {
                  trace("\t","U:",_loc6_.U);
               }
               trace("CUM:\t",_loc3_.CUM);
               for each(_loc7_ in _loc3_.CUM)
               {
                  trace("\t","U:",_loc7_.U);
               }
               _loc2_++;
            }
         }
         trace("********** VAL:",param1.VAL,"**********");
         if(param1.VAL)
         {
            _loc8_ = 0;
            while(_loc8_ < param1.VAL.length)
            {
               trace("---- VAL dataArray at index: ",_loc8_,"----");
               _loc9_ = param1.VAL[_loc8_] as ADData;
               trace("AT:\t",_loc9_.AT);
               trace("RST:\t",_loc9_.RST);
               trace("RS:\t",_loc9_.RS);
               trace("AL:\t",_loc9_.AL);
               trace("CU:\t",_loc9_.CU);
               trace("VT:\t",_loc9_.VT);
               trace("VC:\t",_loc9_.VC);
               trace("W:\t",_loc9_.W);
               trace("H:\t",_loc9_.H);
               trace("CF:\t",_loc9_.CF);
               trace("JS:\t",_loc9_.JS);
               trace("BRS:\t",_loc9_.BRS);
               trace("SU:\t",_loc9_.SU);
               trace("LOC:\t",_loc9_.LOC);
               for each(_loc10_ in _loc9_.SU)
               {
                  trace("\t","U:",_loc10_.U);
                  trace("\t","T:",_loc10_.T);
               }
               trace("SUS:\t",_loc9_.SUS);
               for each(_loc11_ in _loc9_.SUS)
               {
                  trace("\t","U:",_loc11_.U);
               }
               trace("SUE:\t",_loc9_.SUE);
               for each(_loc12_ in _loc9_.SUE)
               {
                  trace("\t","U:",_loc12_.U);
               }
               trace("CUM:\t",_loc9_.CUM);
               for each(_loc13_ in _loc9_.CUM)
               {
                  trace("\t","U:",_loc13_.U);
               }
               _loc8_++;
            }
         }
         trace("********** SEED:",param1.SEED,"**********");
         if(param1.SEED)
         {
            _loc14_ = 0;
            while(_loc14_ < param1.SEED.length)
            {
               trace("---- SEED dataArray at index: ",_loc14_,"----");
               _loc15_ = param1.SEED[_loc14_] as ADData;
               trace("AT:\t",_loc15_.AT);
               trace("RST:\t",_loc15_.RST);
               trace("RS:\t",_loc15_.RS);
               trace("AL:\t",_loc15_.AL);
               trace("CU:\t",_loc15_.CU);
               trace("VT:\t",_loc15_.VT);
               trace("VC:\t",_loc15_.VC);
               trace("W:\t",_loc15_.W);
               trace("H:\t",_loc15_.H);
               trace("CF:\t",_loc15_.CF);
               trace("JS:\t",_loc15_.JS);
               trace("BRS:\t",_loc15_.BRS);
               trace("SU:\t",_loc15_.SU);
               trace("LOC:\t",_loc15_.LOC);
               for each(_loc16_ in _loc15_.SU)
               {
                  trace("\t","U:",_loc16_.U);
                  trace("\t","T:",_loc16_.T);
               }
               trace("SUS:\t",_loc15_.SUS);
               for each(_loc17_ in _loc15_.SUS)
               {
                  trace("\t","U:",_loc17_.U);
               }
               trace("SUE:\t",_loc15_.SUE);
               for each(_loc18_ in _loc15_.SUE)
               {
                  trace("\t","U:",_loc18_.U);
               }
               trace("CUM:\t",_loc15_.CUM);
               for each(_loc19_ in _loc15_.CUM)
               {
                  trace("\t","U:",_loc19_.U);
               }
               _loc14_++;
            }
         }
         trace("********** ATS:",param1.ATS,"**********");
         trace("********** PS:",param1.PS,"**********");
         trace("// ----------------------------------------------- //");
      }
   }
}
